<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible="showExcelDialog"
    @close="$emit('update:showExcelDialog', false)"
  >
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <!-- accept 属性限制上传的文件类型 -->
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls"
          @change="upload"
        >
        <!--
            拖拽事件
            1. dragenter 拖拽进入
            2. dragover  拖拽进入后 没有松开鼠标 持续触发
            3. drop      在拖拽区域内松手
            4. dragleave  拖拽离开
         -->
        <div
          class="drop"
          :class="{bd: isAdd}"
          @dragenter="drag"
          @dragover="drag"
          @drop="drop"
          @dragleave="dragleave"
        >
          <i class="el-icon-upload" />
          <el-button type="text" @click="download">下载导入模板</el-button>
          <span>将文件拖到此处或
            <el-button type="text" @click="handleClick">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <!-- update:props属性名，值 直接修改 .sync修饰符的属性值 -->
      <el-button size="mini" type="primary" @click="$emit('update:showExcelDialog', false)">取消</el-button>
    </el-row>
  </el-dialog>
</template>
<script>
import { exportTemplate, uploadEmployee } from '@/api/employee'
import { saveAs } from 'file-saver'
export default {
  props: {
    showExcelDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isAdd: false
    }
  },
  methods: {
    async  download() {
      // 1. 获取模板的excel
      const res = await exportTemplate()
      // 2. 使用file-saver的saveAs方法
      saveAs(res, '导入员工模板.xlsx')
    },
    // 点击按钮触发文件域的click事件
    handleClick() {
      this.$refs['excel-upload-input'].click()
    },
    // 上传
    upload(e) {
      if (!e.target.files[0]) return
      // console.log(e.target.files[0])
      // console.log('发送请求')
      // 选择完毕发送请求
      this.uploadMethod(e.target.files[0])
    },
    drag(e) {
      e.preventDefault()
      e.stopPropagation()
      this.isAdd = true
      // console.log('拖拽进入')
    },
    drop(e) {
      e.preventDefault()
      e.stopPropagation()
      // 松手发送请求
      this.uploadMethod(e.dataTransfer.files[0])
    },
    async uploadMethod(file) {
      const fm = new FormData()
      fm.append('file', file)
      // 2. 发送请求
      await uploadEmployee(fm)
      // 4. 关闭对话框
      this.$emit('update:showExcelDialog', false)
      // 5. 通知父组件重新获取最新的员工
      this.$emit('reload')
      // 6. 提示
      this.$message.success('上传成功')
      // 7. 文件域的value值清空
      this.$refs['excel-upload-input'].value = null
    },
    dragleave(e) {
      e.preventDefault()
      e.stopPropagation()
      this.isAdd = false
    }
  }
}
</script>

<style scoped lang="scss">
  .bd {
     border: 1px dashed aqua !important;
  }
    .upload-excel {
      display: flex;
      justify-content: center;
      margin: 20px;
      width: 360px;
      height: 180px;
      align-items: center;
      color: #697086;
      .excel-upload-input {
        display: none;
        z-index: -9999;
      }
      .btn-upload,
      .drop {
        border: 1px dashed #dcdfe6;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 160px;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .drop {
        line-height: 40px;
        color: #bbb;
        i {
          font-size: 60px;
          display: block;
          color: #c0c4cc;
        }
      }
    }
</style>
